# -*- coding: utf-8 -*-

from rest_framework import serializers
from apps.sys.models import File
from core.serializers import CustomModelSerializer


class FileSerializer(CustomModelSerializer):
    file = serializers.SerializerMethodField(read_only=True)

    def get_file(self, instance):
        return "/media/" + str(instance.file)

    class Meta:
        model = File
        fields = "__all__"

    def create(self, validated_data):
        validated_data["name"] = self.initial_data.get("file").name
        validated_data["file"] = self.initial_data.get("file")
        validated_data["size"] = self.initial_data.get("file").size
        return super().create(validated_data)
